package com.qidianit.mes.pro.service.impl;

import com.qidianit.mes.pojo.CodeAuto;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.mapper.CodeAutoMapper;
import com.qidianit.mes.pro.service.CodeAutoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/26 -10:02
 */
@Service
public class CodeAutoServiceImpl implements CodeAutoService {
    @Autowired
    private CodeAutoMapper codeAutoMapper;

    @Override
    public Result getCodeAuto(String type) {
        //生成对应的编码
        StringBuilder stringBuilder = new StringBuilder();
        //1、获取对应的前缀
        CodeAuto codeAuto = codeAutoMapper.queryByType(type);
        stringBuilder.append(codeAuto.getPrefix());
        //2、获取年月日
        stringBuilder.append(new SimpleDateFormat("yyMMdd").format(new Date()));
        //3、获取序列
        Integer sequence = codeAuto.getSequence();
        for (int i=0;i<4-sequence.toString().length();i++){
            stringBuilder.append("0");
        }
        stringBuilder.append(sequence.toString());
        //编码完成后，更新数据库序列
        codeAuto.setSequence(sequence+1);
        codeAutoMapper.updateCodeAuto(codeAuto);
        return Result.success(stringBuilder);
    }
}
